﻿using System;
using System.Collections.Generic;
using System.Text;


using System.Data;

using com.dz.ht.db;
using com.dz.ht.common;

namespace com.dz.ht.components
{
    public  class DataManger
    {
        BookDao m_book_dao = new BookDao();
          /// <summary>
        /// 添加书的信息
        /// </summary>
        public long InsertBookInfo(BookInfo model,out string o_last_chapter_name)
        {
            o_last_chapter_name = string.Empty;
            try
            {
                return m_book_dao.InsertBookInfo(model,out o_last_chapter_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加书籍信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

         /// <summary>
        /// 添加章节信息
        /// </summary>
        /// <param name="i_volume_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <param name="i_update_time"></param>
        /// <param name="i_is_vip"></param>
        /// <param name="i_words_count"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public long InsertBookChapter(BookChapterInfo i_book_chapter_info,string i_last_chapter_name)
        {
            try
            {
                return m_book_dao.InsertBookChapter(i_book_chapter_info,i_last_chapter_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加章节信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

            /// <summary>
        /// 获取更新书籍
        /// </summary>
        /// <param name="i_site_id">站点id</param>
        /// <param name="i_type">-1全部书籍 0 上次抓取记录下的bookid开始</param>
        /// <param name="i_count">取多少条记录</param>
        /// <param name="i_order_type">1升序 0 降序</param>
        /// <returns></returns>
        public DataSet GetBook(Consts.Site i_site_id, int i_type, int i_count, int i_order_type)
        {
            try
            {
                return m_book_dao.GetBook(i_site_id, i_type, i_count, i_order_type);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取更新书籍失败" + ex.ToString());
                return null;
            }
        }

          /// <summary>
        /// 获取需要更新的章节
        /// </summary>
        /// <returns></returns>
        public DataSet GetChapter(int i_site_id)
        {
            try
            {
                return m_book_dao.GetChapter(i_site_id);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取需要更新章节列表失败" + ex.ToString());
                return null;
            }
        }

             /// <summary>
        /// 更新章节状态
        /// </summary>
        /// <param name="t_book_id"></param>
        /// <param name="t_chapter_id"></param>
        /// <returns></returns>
        public int UpdateChapterStatus(long t_book_id, long t_chapter_id)
        {
            try
            {
                return m_book_dao.UpdateChapterStatus(t_book_id, t_chapter_id);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新章节状态失败" + ex.ToString());
                return Consts.Faild;
            }
        }

          /// <summary>
        /// 插入书籍评论
        /// </summary>
        /// <param name="i_review_content"></param>
        /// <returns></returns>
        public long InsertBookReview(long i_book_id, string i_review_content)
        {
            try
            {
                return m_book_dao.InsertBookReview(i_book_id, i_review_content);
            }
            catch (Exception ex)
            {
                LogHelper.Error("插入书籍评论失败" + ex.ToString());
                return Consts.Faild;
            }
        }

          /// <summary>
        /// 获取需要收集评论的书籍
        /// </summary>
        /// <param name="i_type">1 升序 0 降序 其他为bookid开始值</param>
        /// <returns></returns>
        public DataSet GetBookByReview(int i_type)
        {
            try
            {
                return m_book_dao.GetBookByReview(i_type);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取需要收集评论的书籍失败" + ex.ToString());
                return null;
            }
        }

          /// <summary>
        /// 获取不存在封面的书籍
        /// </summary>
        /// <returns></returns>
        public DataSet GetBookICON()
        {
            try
            {
                return m_book_dao.GetBookICON();
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取没有封面的书籍失败" + ex.ToString());
                return null;
            }
        }

             /// <summary>
       /// 更新小说收集章节配置
       /// </summary>
       /// <param name="i_site_info"></param>
       /// <returns></returns>
        public int UpdateBookSearchSite(SearchSiteInfo i_site_info)
        {
            try
            {
                int ret = m_book_dao.UpdateBookSearchSite(i_site_info);
                if (ret > 0)
                    return Consts.Success;
                return Consts.Faild;
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新小说收集配置失败" + ex.ToString());
                return Consts.Faild;
            }
        }

           /// <summary>
        /// 查询小说
        /// </summary>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public DataSet QueryData(string i_str_sql)
        {
            try
            {
                return m_book_dao.QueryData(i_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询小说列表失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获取VIP书籍列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetVIPBookList()
        {
            string t_str_sql = "select a.bookId,a.chapterUrl searchUrl,b.lastUpdateChapterName,b.bookCategoryId, b.chapterUrl,c.* from tbbooksearch a,tbbook b,tbbookSite c where a.bookid=b.bookid and a.siteId=c.siteId ";

            try
            {
                return m_book_dao.QueryData(t_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询VIP小说列表失败" + ex.ToString());
                return null;
            }
        }

         /// <summary>
      /// 跟新小说收集错误信息
      /// </summary>
      /// <param name="i_book_id"></param>
      /// <param name="i_error_count"></param>
      /// <returns></returns>
        public int UpdateBookSearchError(long i_book_id, int i_error_type)
        {
            try
            {
                return m_book_dao.UpdateBookSearchError(i_book_id, i_error_type);
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集小说章节信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

           /// <summary>
        /// 更新小说章节更新地址
        /// </summary>
        /// <param name="i_site_id"></param>
        /// <param name="i_book_name"></param>
        /// <param name="i_chapter_url"></param>
        /// <returns></returns>
        public long UpdateBookChapterUrl(int i_site_id, string i_book_name, string i_chapter_url)
        {
            try
            {
                return m_book_dao.UpdateBookChapterUrl(i_site_id, i_book_name, i_chapter_url);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新小说章节更新地址失败" + ex.ToString());
                return Consts.Faild;
            }
        }

        /// <summary>
       /// 添加小说收集信息
       /// </summary>
       /// <param name="i_book_id"></param>
       /// <param name="i_chapter_url"></param>
       /// <param name="i_site_id"></param>
       /// <returns></returns>
        public int AddBookSearch(string i_book_name, string i_chapter_url, int i_site_id)
        {
            try
            {
                return m_book_dao.AddBookSearch(i_book_name.Trim(), i_chapter_url.Trim(), i_site_id);
               
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加小说收集信息失败" + ex.ToString());
                return -1;
            }
        }
    }
}
